package com.rtz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class SimpleDateFormatUtil {

	/**
	 * 日期合法性验证
	 * 
	 * @param str
	 * @param format
	 * @return
	 */
	public static boolean isValidDate(String date, String format) {
		boolean convertSuccess = true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		try {
			// 设置lenient为false.
			// 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			fmt.setLenient(false);
			fmt.parse(date);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}

		if (format.equals("yyyyMMdd")) {

			return RegularVerificationUtil.date(date);
		}
		return convertSuccess;
	}
	/**
	 * 字符串转日期根据format
	 * 
	 * @param str
	 *            如："2017-05-26 13-38-18"
	 * @param format
	 *            如:"yyyy-MM-dd HH-mm-ss"
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDate(String str, String format) throws ParseException {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		if (str != null) {

			date = sdf.parse(str);

		}

		return date;
	}

	/**
	 * 日期转字符串根据format
	 * 
	 * @param date
	 * @param format
	 *            如:"yyyy-MM-dd HH-mm-ss"
	 * @return
	 */
	public static String foramteDate(Date date, String format) {

		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		if (date != null) {
			result = sdf.format(date);
		}

		return result;

	}

	/**
	 * 获取当前时间的前一天时间
	 * 
	 * @param cl
	 * @return
	 */
	public static String getBeforeDay(Date date, String format) {
		// 使用roll方法进行向前回滚
		// cl.roll(Calendar.DATE, -1);
		// 使用set方法直接进行设置

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day - 1);

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(calendar.getTime());

		return dateString;
	}
	/**
	 * 获取当前系统时间 格式为yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getNowforamteDate() {

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);

	}

	/**
	 * 获取当前系统时间 格式为yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String getNowforamteDate(String formateString) {

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(formateString);
		return sdf.format(date);

	}

	/**
	 * 获取当前时间的前一天时间
	 * 
	 * @param cl
	 * @return
	 */
	public static String getBeforeDay(Date date) {
		// 使用roll方法进行向前回滚
		// cl.roll(Calendar.DATE, -1);
		// 使用set方法直接进行设置

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day - 1);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(calendar.getTime());

		return dateString;
	}

	/**
	 * 获取当前时间的后一天时间
	 * 
	 * @param cl
	 * @return
	 */
	// 使用roll方法进行回滚到后一天的时间
	public static String getAfterDay(Date date) {
		// cl.roll(Calendar.DATE, 1);
		// 使用set方法直接设置时间值
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day + 1);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取当前时间的后一天时间
	 * 
	 * @param cl
	 * @return
	 */
	// 使用roll方法进行回滚到后一天的时间
	public static Date getAfterDayFommatDate(Date date) {
		// cl.roll(Calendar.DATE, 1);
		// 使用set方法直接设置时间值
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day + 1);
		return calendar.getTime();
	}
	public static String getAfterDay2(Date date) {
		// 使用roll方法进行回滚到后一天的时间
		// cl.roll(Calendar.DATE, 1);
		// 使用set方法直接设置时间值
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day + 1);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 两个时间相差天数
	 * 
	 * @param dateBegin
	 * @param dateEnd
	 * @return
	 */
	public static long compareTime(Date dateBegin, Date dateEnd) {
		long between = (dateEnd.getTime() - dateBegin.getTime()) / 1000;// 除以1000是为了转换成秒
		long betweenDay = between / 60 / 60 / 24;
		return betweenDay;
	}

	public static boolean isThisDay(Date thisDay) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		if (fmt.format(thisDay).toString().equals(fmt.format(new Date()).toString())) {// 格式化为相同格式
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 两个时间比较先后
	 * 
	 * @param dateBegin
	 * @param dateEnd
	 * @return
	 */
	public static boolean isBeginBeforeEnd(Date dateBegin, Date dateEnd) {
		if (dateBegin.getTime() <= dateEnd.getTime()) {
			return true;
		} else {
			return false;
		}
	}
	public static void main(String[] args) throws ParseException {
		System.out.println(dateBelongTo("2018-01-07", "02"));

	}

	public static String dateBelongTo(String dateString, String dateType) {
		String date = "";
		switch (dateType) {
			case "00" :
				date = dateString;
				break;
			case "01" :
				date = dateString.substring(0, 7);
				break;
			case "02" :
				date = dateString.substring(0, 4);
				String month = dateString.substring(5, 7);
				String quarter = String.valueOf((int) Math.ceil(Double.parseDouble(month) / 3));
				date = date + "0" + quarter;
				break;
			case "03" :
				date = dateString.substring(0, 4);
				break;
		}
		return date;
	}
	public static String getStart(String end, String type) {
		switch (type) {
			case "00" :
				return end.substring(0, 7) + "-01 00:00:00";
			case "01" :
				return end.substring(0, 7) + "-01 00:00:00";
			case "02" :
				if ("01".equals(end.substring(5, 7)) || "02".equals(end.substring(5, 7)) || "03".equals(end.substring(5, 7)))
					return end.substring(0, 4) + "-01-01 00:00:00";
				else if ("04".equals(end.substring(5, 7)) || "05".equals(end.substring(5, 7)) || "06".equals(end.substring(5, 7)))
					return end.substring(0, 4) + "-04-01 00:00:00";
				else if ("07".equals(end.substring(5, 7)) || "08".equals(end.substring(5, 7)) || "09".equals(end.substring(5, 7)))
					return end.substring(0, 4) + "-07-01 00:00:00";
				else if ("10".equals(end.substring(5, 7)) || "11".equals(end.substring(5, 7)) || "12".equals(end.substring(5, 7)))
					return end.substring(0, 4) + "-10-01 00:00:00";
				else
					return "";
			case "03" :
				return end.substring(0, 4) + "-01-01 00:00:00";
			default :
				return "";
		}
	}

	
	public static boolean dailyFlag(String dateString , String dateType){
		boolean flag=true;
		String date="";
    	switch (dateType) {
    	case "00" :
    		flag=true;
    		break;
    	case "01" ://月数据=日数据不重新结算
    		flag=false;
    		break;
    	case "02" :
    		date=dateString.substring(0,4);
    		if(dateString.equals(date+"-12-31")||dateString.equals(date+"-03-31")||dateString.equals(date+"-06-30")||dateString.equals(date+"-09-30")) {
    			flag=true;
    		}else {
    			flag=false;
    		}
    		break;
    	case "03" :
    		date=dateString.substring(0,4)+"-12-31";
    		if(dateString.equals(date)) {
    			flag=true;
    		}else {
    			flag=false;
    		}
    		break;	
    	}
		return flag;
	}

	/**
	 * 获取上一年的时间
	 */
	public static String getLastDateForOneYear(String formatStr) {

		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.YEAR, -1);
		Date y = c.getTime();
		String year = format.format(y);
		return year;

	}
	/**
	 * 获取前一天上一年的时间
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String getLastDateForOneYearAndOneDay(String formatStr) {

		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, -1);
		c.add(Calendar.YEAR, -1);
		Date y = c.getTime();
		String year = format.format(y);
		return year;

	}
	/**
	 * 获取过去七天时间
	 */
	public static String getLatsDateForSevenDays(String formatStr) {

		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, -7);
		Date d = c.getTime();
		String day = format.format(d);
		return day;

	}

	/**
	 * 获取过去一月的时间
	 */
	public static String getLatsDateForOneMonth(String formatStr) {

		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date m = c.getTime();
		String mon = format.format(m);
		return mon;

	}

	/**
	 * 获取过去三个月
	 */
	public static String getLatsDateForThreeMonth(String formatStr) {

		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -3);
		Date m3 = c.getTime();
		String mon = format.format(m3);
		return mon;
	}

}